package com.atguigu.gmall.common.service;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author atguigu-mqx
 */
@Service
public class RabbitService {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    //  编写发送消息的方法
    public Boolean sendMessage(String exchange, String routingKey, Object message){
        //  发送消息
        this.rabbitTemplate.convertAndSend(exchange,routingKey,message);
        //  返回值
        return true;
    }

    //  编写发送延迟消息的方法
    public Boolean sendDelayMessage(String exchange, String routingKey, Object message, int timeDely){
        //  发送消息
        this.rabbitTemplate.convertAndSend(exchange,routingKey,message,(msg)->{
            msg.getMessageProperties().setDelay(timeDely*1000);
            return msg;
        });
        //  返回值
        return true;
    }

}
